#!/bin/bash
SERVER_URL=https://beta.ray0728.cn/localai
MODEL_BACKEND=bert-embeddings
LIST_MODELS=v1/models
LIST_MODELS_AVAILABLE=models/avilable
LIST_MODELS=v1/models
EMBEDDINGS_URI=v1/embeddings
BACKEND_MONITOR_URI=backend/monitor


function listAvailableModels(){
    curl -X 'GET' \
    $SERVER_URL/$LIST_MODELS\
    -H 'accept:application/json'
}

function embedding(){
    curl -X 'POST' \
    $SERVER_URL/$EMBEDDINGS_URI\
    -H 'Content-Type: application/json' \
    -H 'accept:application/json' \
    -d '{
    "backend": "'${MODEL_BACKEND}'",
    "max_tokens":'$1',
    "ignore_eos": true,
    "input": "'${*:2}'"
    }'
}

function getMonitor(){
    curl -X 'GET' \
    $SERVER_URL/$BACKEND_MONITOR_URI \
    -H 'Content-Type: application/json' \
    -H 'accept:application/json' \
    -d '{
    "model":'$1'
    }'
}

echo ""
while getopts "e:lh" opt
do
    case $opt in
	l)
	result=`listAvailableModels | jq '.data[].id' `
	for model in ${result[@]};
	do
	    echo $model
	    echo "------------"
	    getMonitor $model
	    echo
	done
	;;
        e)
        embedding ${*:2}
	;;
        h)
	echo "usage: $0 [-e][-l]"
	echo "-e embedde your specifiy message."
	echo "-e <max tokens> <message>"
	echo "eg. $0 -e 128 A long time ago in a galaxy far,far away."
	echo "-l list installable models"
	echo "eg. $0 -l"
	exit 1
	;;
esac done
#getAvailableModels
